<template>
    <div style="padding:20px;">
        <Row :gutter="20">
            <Col span="4" v-if="personType === 1">
                <Select v-model="parmes.whereMap.orgId" ref="treeSelect" placeholder="请选择组织机构">
                    <Option v-for="item in list2" :value="item.enterpriseId" :key="item.enterpriseId" style="display: none;" >{{ item.title }}</Option>
                    <Tree :data="entTree" ref="tree" style="padding-left:20px" @on-select-change="checkEnt"></Tree>
                </Select>
            </Col>
            <Col span="4">
                <Select v-model="parmes.whereMap.proId" placeholder="请选择所属项目" :clearable="proList.length>0 && personType !=3">
                    <Option v-for="item in proList" :value="item.id" :key="item.id">
                        {{item.shortName}}
                    </Option>
                </Select>
            </Col>
            <Col span="4">
                <Select v-model="parmes.whereMap.businessType" placeholder="请选择回复业务类型" clearable>
                    <Option v-for="item in businessType" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
            </Col>
            <Col span="4">
                <Select v-model="parmes.whereMap.riskLevel" placeholder="请选择风险等级" clearable>
                    <Option v-for="item in riskGradeList" :value="item.codes" :key="item.codes">{{ item.name }}</Option>
                </Select>
            </Col>
            <Col span="4">
               <!-- <Select v-model="parmes.whereMap.riskCategoryIds" ref="treeRiskType" placeholder="请选择风险类型" clearable>
                    <Option v-for="item in treeRiskType" :value="item.id" :key="item.id" style="display: none;">{{ item.title }}</Option>
                    <Tree :data="treeRiskTypeData" style="padding-left:20px" @on-select-change="checkData"></Tree>
                </Select> -->
                <Select v-model="parmes.whereMap.riskCategoryIds" clearable placeholder="请选择风险类别">
                    <Option v-for="item in treeRiskType" :value="item.id" :key="item.id" >{{ item.riskCategoryName }}</Option>
                </Select>
            </Col>
            <Col :span="personType === 1 ? '4' : '8'" style="text-align:right;">
                <Button type="primary" @click="questionList()">查询</Button>
            </Col>
        </Row>
        <Table style="margin-top: 20px;" border ref="selection" :columns="columns4" :data="data1">
            <template slot-scope="{ row, index }" slot="type">
                <Button type="primary" size="small">{{row.businessTypeName}}</Button>
            </template>
            <template slot-scope="{ row, index }" slot="action">
                <router-link :to="{name:'replyInquiries-detail',query:{'source':'1','businessType':row.businessType,'objectId':row.objectId,'id':row.id,'riskName':row.businessType == 'businesstype_control' ? row.measuresName : row.riskName}}" style="padding-right:10px">回复</router-link>
                <a @click="detailLink(row)">详情</a>
            </template>
        </Table>
        <div class="page-div">
            <Page
            class="tab-page"
            :total="total"
            :current="parmes.currentPage"
            :page-size="parmes.pageSize"
            @on-change="pageChange"
            @on-page-size-change="sizeChange"
            show-elevator
            show-sizer
            :page-size-opts='[10,20,50,100]'
            show-total
            />
        </div>
    </div>
</template>
<script>
import crudMixin from '../../mixins/crudMixin'
export default {
    mixins: [crudMixin],
    name:'service_enquiries-detail',
    data(){
        return{
            model1:'',
            list2: [], //管理单位SELECT选择
            entTree:[],
            personType:JSON.parse(localStorage.getItem('personType')),
            userDetail:JSON.parse(localStorage.getItem('userList')),
            businessType: [
                {
                    value: 'businesstype_identification',
                    label: '风险辨识'
                },
                {
                    value: 'businesstype_measures',
                    label: '管控措施'
                },
                {
                    value: 'businesstype_elimination',
                    label: '辨识销号'
                },
                {
                    value: 'businesstype_dynamics',
                    label: '每月动态'
                },
                {
                    value: 'businesstype_control',
                    label: '过程管控'
                }
            ],
            columns4: [
                {
                    title: '序号',
                    align: 'center',
                    width: 60,
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.parmes.currentPage - 1) * this.parmes.pageSize + 1
                        )
                    }
                },
                {
                    title: '公司名称',
                    key: 'orgName',
                    align: 'center'
                },
                {
                    title: '项目名称',
                    key: 'proName',
                    align: 'center'
                },
                {
                    title: '工点名称',
                    key: 'constructionPointName',
                    align: 'center'
                },
                {
                    title: '作业风险类别',
                    key: 'riskCategoryName',
                    align: 'center'
                },
                {
                    title: '作业风险名称',
                    key: 'riskName',
                    align: 'center'
                },
                {
                    title: '作业风险等级',
                    key: 'riskLevelName',
                    align: 'center'
                },
                {
                    title: '回复业务类型',
                    slot: 'type',
                    align: 'center'
                },
                {
                    title: '操作',
                    slot: 'action',
                    width: 100,
                    align: 'center'
                },
            ],
            data1: [],
            total:0,
            parmes:{
                currentPage:1,
                pageSize:10,
                orderColumn:'',
                whereMap:{
                    orgId:'',
                    proId:'',
                    businessType:'',
                    riskLevel:'',
                    riskCategoryIds:'',
                    appId:'pc'
                }
            },
            riskGradeList:[],
            treeRiskTypeData: [], //风险类型树
            treeRiskType:[],
            treeList: [],
            treeName:'',
            proList:[],
        }
    },
    mounted(){
        this.getRiskGrade()
        this.getRiskType()
        this.projectGetAll()
        this.questionList()
        //1集团 2工程公司 3项目部
        if (this.personType === 3) {
            this.getProTree({objectId:this.userDetail.projectTeamId})
        }else if(this.personType === 1){
            this.getOrgTree();
        }else if(this.personType === 2){
            this.getProTree({objectId:this.userDetail.enterpriseId})
        }
    },
    methods:{
        //获取纯机构树
        getOrgTree() {
            this.entTree = [];
            this.list2 = [];
            this.$api.measurePlan.getOrgTree().then((res) => {
                if (res.code == 200) {
                    this.entTree = res.data
                    const tree = this.$refs.tree
                    this.$nextTick(() =>  tree.handleSelect(tree.data[0].nodeKey))
                } else {
                    this.$Message.error(res.data.msg);
                }
            })
        },
        checkEnt(data) {
            if (data.length > 0) {
                this.parmes.whereMap.proId = '';
                this.list2 = []
                let title = data[0].title
                if(data[0].objectId){
                    let enterpriseId = data[0].objectId
                    this.list2.push({
                        title,
                        enterpriseId
                    })
                    this.getProTree(data[0]);
                    this.parmes.whereMap.orgId = enterpriseId
                    this.$refs.treeSelect.visible = false
                }else {
                    this.$Message.warning('请选择'+title+'下的子集')
                }
            }
        },
        //获取项目树
        getProTree(data) {
            this.proList = [];
            this.$api.measurePlan.getProList(data.objectId).then(res => {
                if (res.code === 200) {
                    if(res.data.length>0){
                        this.proList = res.data;
                        if(this.personType == 3){
                            this.parmes.whereMap.proId = this.proList[0].id;
                        }else if(this.personType == 2){
                            this.parmes.whereMap.orgId = data.objectId;
                        }else{
                            this.parmes.whereMap.proId = '';
                        }
                    }
                } else {
                    this.$Message.error(res.message)
                }
            })
        },
        // 详情跳转
        detailLink(item){
            if(item.businessType == 'businesstype_identification'){     // 风险辨识
                this.$router.push({
                    name:'risk-identification-detail',
                    params:{
                        id:item.objectId
                    },
                    query:{
                        message:true,
                        source:1,
                        businessType:item.businessType,
                        objectId:item.objectId,
                        id:item.id,
                        riskName:item.riskName
                    }
                })
            }else if(item.businessType == 'businesstype_measures'){     // 管控措施
                this.$router.push({
                    name: `management-and-measures-issue`,
                    params:{
                        type:'details',
                        id:item.objectId,
                        status:5,
                        name:'方案详情'
                    },
                    query:{
                        message:true,
                        source:1,
                        businessType:item.businessType,
                        objectId:item.objectId,
                        id:item.id,
                        riskName:item.riskName
                    }
                })
            }else if(item.businessType == 'businesstype_elimination'){     // 辨识销号
                this.$router.push({
                    name: `risk-elimination-detail`,
                    params:{
                        type:'details',
                        id:item.objectId,
                        name:'销号详情'
                    },
                    query:{
                        message:true,
                        source:1,
                        businessType:item.businessType,
                        objectId:item.objectId,
                        id:item.id,
                        riskName:item.riskName
                    }
                })
            }else if(item.businessType == 'businesstype_dynamics'){      // 每月动态
                this.$router.push({
                    name: `dynamics-detail`,
                    query:{
                        businessType:item.businessType,
                        objectId:item.objectId,
                        id:item.id,
                        riskName:item.riskName
                    }
                })
            }else if(item.businessType == 'businesstype_control'){     // 过程管控
                this.$router.push({
                    name: 'procedure-control-detail',
                    params: {
                        id:item.objectId
                    },
                    query:{
                        message:true,
                        source:1,
                        businessType:item.businessType,
                        objectId:item.objectId,
                        id:item.id,
                        riskName:item.measuresName
                    }
                })
            }
        },
        //获取风险等级
        getRiskGrade() {
            let code = 'RISK_GRADE'
            this.$api.projectInquiry.getListByTypeCode(code).then(res => {
                if (res.code == 200) {
                    res.data.forEach((e,index)=>{
                        e.codes = (index+1)
                    })
                    this.riskGradeList = res.data
                } else {
                    this.$Message.error(res.data.msg)
                }
            })
        },
        //获取风险类别
        getRiskType() {
            this.$api.measuresLibrary.getRiskCategoryFindAll().then(res => {
                if (res.code == 200) {
                    this.treeRiskType = res.data
                } else {
                    this.$Message.error(res.message)
                }
            })
        },
        changeRishTypeName(data) {
            if (data) {
                let item = data
                const arr = []
                item.forEach(val => {
                    let obj = {}
                    obj.title = val.name
                    obj.id = val.id
                    obj.children = []
                    if (val.children.length > 0) {
                        obj.children = this.changeRishTypeName(val.children)
                    }
                    arr.push(obj)
                })
                return arr
            }
        },
        checkData(data){
            this.treeRiskType = []
            this.parmes.whereMap.riskCategoryIds = data[0].id
            this.treeRiskType.push({
                id:data[0].id,
                title:data[0].title
            })
            this.$refs.treeRiskType.visible = false
        },
        // 问询列表
        questionList(){
            this.$api.messageManage.questionList(this.parmes).then(resp=>{
                if(resp.code == 200){
                    this.data1 = resp.data.resultObject
                    this.total = resp.data.totalRecord
                }
            })
        },
        // 分页
        pageChange(page){
            this.parmes.currentPage = page
            this.questionList()
        },
        // 分页
        sizeChange(num){
            this.parmes.pageSize = num
            this.questionList()
        },
    }
}
</script>